package com.sxhuayuan.parking.controller.parkinglot.user;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.dto.SumDTO;
import com.sxhuayuan.parking.entity.ParkChannel;
import com.sxhuayuan.parking.entity.ParkingLotUser;
import com.sxhuayuan.parking.service.*;
import com.sxhuayuan.parking.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 统计
 */
@RestController("parkingLotUserStatisticsController")
@RequestMapping("/parkinglotuser/statis")
public class StatisticsController extends BaseController {

	@Autowired
	OrderService orderService;
	@Autowired
	ParkingLotUserService parkingLotUserService;
	@Autowired
	ParkingLotService parkingLotService;
	@Autowired
	RedisService redisService;
	@Autowired
	ParkChannelService parkingLotExitService;

	/**
	 * 统计信息的redis key=s+id
	 */
	private static final String REDIS_PARKING_ORDER_COUNT = "parkingLotUser_order_count_";

	/**
	 * 统计本月与昨天一天订单金额
	 * 
	 */
	@GetMapping("/order")
	public RespMessage countOrder() {

		ParkingLotUser current = parkingLotUserService.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		// 先从redis里获取统计信息
		String string = redisService.getString(REDIS_PARKING_ORDER_COUNT + current.getId());
		if (string != null) {
			List<SumDTO> list = JSON.parseArray(string, SumDTO.class);
			return RespMessage.builder().success().setDatas(list).build();
		}

		Date yesterdayStartTime = DateUtils.getStartTimeOfOneday(-1);
		Date startTimeOfCurrentMonth = DateUtils.getStartTimeOfCurrentMonth();
		Date yesterdayEndTime = DateUtils.getEndTimeOfOneday(-1);
		// 本月订单统计
		SumDTO sumDTO = orderService.countByParkingLot(startTimeOfCurrentMonth, yesterdayEndTime, current);

		// 昨日订单统计
		SumDTO sumDTO1 = orderService.countByParkingLot(yesterdayStartTime, yesterdayEndTime, current);

		// 统计车场数量与、车场出口数量
		Long aLong = parkingLotExitService.countByParkingLot(current, ParkChannel.Type.exit);

		SumDTO sumDTO2 = new SumDTO();
		sumDTO2.setCount(aLong);
		sumDTO2.setAmount(new BigDecimal(current.getParkingLots().size()));
		List<Object> list = new ArrayList<>();
		list.add(sumDTO);
		list.add(sumDTO1);
		list.add(sumDTO2);
		String s = JSON.toJSONString(list);
		// 保存到redis里 超时时间为当天23:59:59
		redisService.saveString(REDIS_PARKING_ORDER_COUNT + current.getId(), s, DateUtils.nowToTomorrowSecond());

		return RespMessage.builder().success().setDatas(list).build();

	}

	/**
	 * 订单统计
	 *
	 * @param type
	 *            时间格式化类型
	 * @param startTime
	 *            起始时间
	 * @param endTime
	 *            终止时间
	 * @param id
	 *            车场id
	 * @return 统计结果 0:时间 1:订单数量 3:订单金额
	 */
	@PostMapping("/echars/countorder")
	public RespMessage cylindricalGraph(@RequestParam String type, @RequestParam String startTime, @RequestParam String endTime, Long id) {

		ParkingLotUser current = parkingLotUserService.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		String sn = null;
		if (id != null) {
			sn = parkingLotService.findOne(id).getSn();
		}

		String formatType = null;
		switch (type) {
		case "date":
			formatType = "%m-%d";
			break;
		case "month":
			formatType = "%Y-%m";
			break;
		default:
			RespMessage.error("查询类型错误");
		}
		List<Map<String, Object>> map = orderService.findCountByDateTime(formatType, startTime, endTime, sn, null);
		return successResp(map);
	}

	/** 查询近30天订单统计数据 折线图*/
	@GetMapping("/echars/brokenlinediagram")
	public RespMessage brokenLineDiagram() {
		ParkingLotUser current = parkingLotUserService.getCurrent();
		if (current == null) {
			return invalidToken();
		}

		String redis_order_statis = "parkingLot_order_month_";
		String string = redisService.getString(redis_order_statis + current.getId());
		if (string != null) {
			return successResp(JSON.parseArray(string));
		}
		// 30天以前的00:00:00
		String startTime = DateUtils.format(DateUtils.getStartTimeOfOneday(-30));
		// 前一天的23:59:59
		String endTime = DateUtils.format(DateUtils.getEndTimeOfOneday(-1));
		List<Map<String, Object>> map = orderService.countByDateTimeAndParkingLotUser(startTime, endTime, current);
		String s = JSON.toJSONString(map);
		redisService.saveString(redis_order_statis + current.getId(), s, DateUtils.nowToTomorrowSecond());
		return successResp(map);
	}

}
